import requests

class TestIHRMEmy:

    def test01_login_success(self):
        url = "http://ihrm-test.itheima.net/api/sys/login"
        login_data = {
            "mobile": "13800000002",
            "password": "123456"
        }
        response = requests.post(url=url,json=login_data)
        print(response.json())

        # 断言响应状态码
        assert response.status_code == 200
        # 断言json()返回数据中的success值
        assert response.json().get("success") == True
        # 断言json()返回数据中的code值
        assert response.json().get("code") == 10000
        # 断言返回数据中包含指定消息message
        assert "成功" in response.text
        global token66
        token66 = response.json().get("data")
        print(token66)
    def test02_add(self):

        url_add = "http://ihrm-test.itheima.net/api/sys/user"

        data_header = {
            "Authorization": token66
        }
        data_add = {
            "username": "jack070901160001",
            "mobile": "13702305171",
            "workNumber": "2020010"
        }

        response = requests.post(url=url_add,headers=data_header,json=data_add)
        print(response.json())

        # 断言响应状态码
        assert response.status_code == 200
        # 断言json()返回数据中的success值
        assert response.json().get("success") == True
        # 断言json()返回数据中的code值
        assert response.json().get("code") == 10000
        # 断言返回数据中包含指定消息message
        assert "成功" in response.text

        global id66
        id66 = response.json().get("data").get("id")
        print(id66)
    # 员工查询
    def test03_select(self):
        url_select = f"http://ihrm-test.itheima.net/api/sys/user/{id66}"
        data_header = {
            "Authorization": token66
        }
        response = requests.get(url=url_select,headers=data_header)
        print(response.json())

        # 断言响应状态码
        assert response.status_code == 200
        # 断言json()返回数据中的success值
        assert response.json().get("success") == True
        # 断言json()返回数据中的code值
        assert response.json().get("code") == 10000
        # 断言返回数据中包含指定消息message
        assert "成功" in response.text

    # 员工修改
    def test04_update(self):
        url_add = f"http://ihrm-test.itheima.net/api/sys/user/{id66}"

        data_header = {
            "Authorization": token66
        }
        data_update = {
            "username": "jerry070901160001",
        }

        response = requests.put(url=url_add, headers=data_header, json=data_update)
        print(response.json())

        # 断言响应状态码
        assert response.status_code == 200
        # 断言json()返回数据中的success值
        assert response.json().get("success") == True
        # 断言json()返回数据中的code值
        assert response.json().get("code") == 10000
        # 断言返回数据中包含指定消息message
        assert "成功" in response.text

    def test05_delete(self):
        url_delete = f"http://ihrm-test.itheima.net/api/sys/user/{id66}"
        data_header = {
            "Authorization": token66
        }
        response = requests.delete(url=url_delete, headers=data_header)
        print(response.json())

        # 断言响应状态码
        assert response.status_code == 200
        # 断言json()返回数据中的success值
        assert response.json().get("success") == True
        # 断言json()返回数据中的code值
        assert response.json().get("code") == 10000
        # 断言返回数据中包含指定消息message
        assert "成功" in response.text

